home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xinetd.2.0.6 / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  6.0 KB  |  284 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: log.c,v 5.2 1992/11/17 06:34:06 panos Exp $" ;
  8.  
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/wait.h>
  12. #include <syslog.h>
  13. #include <time.h>
  14.  
  15. #include "sio.h"
  16. #include "str.h"
  17.  
  18. #include "defs.h"
  19. #include "access.h"
  20. #include "logoptions.h"
  21. #include "server.h"
  22. #include "state.h"
  23.  
  24. void msg() ;
  25.  
  26. #define CHECK_LOGSIZE                    TRUE
  27. #define DONT_CHECK_LOGSIZE                FALSE
  28.  
  29. #define LOGBUF_SIZE                        1024
  30.  
  31.  
  32. char *inet_ntoa() ;
  33. time_t time() ;
  34.  
  35.  
  36. /*
  37.  * This function writes log records of the form:
  38.  *
  39.  *        START: service pid [from_address] [time]
  40.  *
  41.  */
  42. void log_success( serp )
  43.     struct server *serp ;
  44. {
  45.     char buf[ LOGBUF_SIZE ] ;
  46.     int bufsize ;
  47.     struct service *sp = SERVER_SERVICE( serp ) ;
  48.     register struct service_config *scp = CONF( sp ) ;
  49.     register int len ;
  50.     register int cc ;
  51.     int log_common() ;
  52.  
  53.     if ( ! SVC_IS_LOGGING( sp ) || M_ARE_ALL_CLEAR( scp->log_on_success ) )
  54.         return ;
  55.     
  56.     bufsize = sizeof( buf ) ;
  57.     len = 0 ;
  58.     
  59.     cc = strx_nprint( buf, bufsize, "START: %s", scp->id ) ;
  60.     len += cc ;
  61.     bufsize -= cc ;
  62.  
  63.     if ( M_IS_SET( scp->log_on_success, LO_PID ) )
  64.     {
  65.         cc = strx_nprint( &buf[ len ], bufsize, " pid=%d", serp->pid ) ;
  66.         len += cc ;
  67.         bufsize -= cc ;
  68.     }
  69.  
  70.     cc = log_common( &scp->log_on_failure, &buf[len], bufsize,
  71.                                                     SERVER_CONNECTION( serp ) ) ;
  72.     len += cc ;
  73.     bufsize -= cc ;
  74.  
  75.     xlog_write( SDATA( sp )->log_handle, buf, len, XLOG_NOFLAGS ) ;
  76. }
  77.  
  78.  
  79. /*
  80.  * This function writes log records of the form:
  81.  *
  82.  *        FAIL: service failure-type [from_address] [time]
  83.  *
  84.  */
  85. void log_failure( access_failure, sp, cp )
  86.     access_e access_failure ;
  87.     register struct service *sp ;
  88.     connection_s *cp ;
  89. {
  90.     char buf[ LOGBUF_SIZE ] ;
  91.     int bufsize ;
  92.     register struct service_config *scp = CONF( sp ) ;
  93.     register int len = 0 ;
  94.     register int cc ;
  95.     int log_common() ;
  96.     void remote_user_logging() ;
  97.     
  98.     if ( ! SVC_IS_LOGGING( sp ) || M_ARE_ALL_CLEAR( scp->log_on_failure ) )
  99.         return ;
  100.     
  101.     bufsize = sizeof( buf ) ;
  102.     cc = strx_nprint( buf, bufsize, "FAIL: %s", scp->id ) ;
  103.     len += cc ;
  104.     bufsize -= cc ;
  105.  
  106.     switch ( access_failure )
  107.     {
  108.         case AC_FORK:
  109.             cc = strx_nprint( &buf[ len ], bufsize, " fork" ) ;
  110.             break ;
  111.         
  112.         case AC_TIME:
  113.             cc = strx_nprint( &buf[ len ], bufsize, " time" ) ;
  114.             break ;
  115.  
  116.         case AC_ADDRESS:
  117.             cc = strx_nprint( &buf[ len ], bufsize, " address" ) ;
  118.             break ;
  119.  
  120.         case AC_SERVICE_LIMIT:
  121.             cc = strx_nprint( &buf[ len ], bufsize, " service_limit" ) ;
  122.             break ;
  123.         
  124.         case AC_PROCESS_LIMIT:
  125.             cc = strx_nprint( &buf[ len ], bufsize, " process_limit" ) ;
  126.             break ;
  127.         
  128.         default:
  129.             cc = strx_nprint( &buf[ len ], bufsize, " BAD_ENUM_VALUE" ) ;
  130.     }
  131.     len += cc ;
  132.     bufsize -= cc ;
  133.  
  134.     cc = log_common( &scp->log_on_failure, &buf[ len ], bufsize, cp ) ;
  135.     len += cc ;
  136.     bufsize -= cc ;
  137.  
  138.     xlog_write( SDATA( sp )->log_handle, buf, len, XLOG_NOFLAGS ) ;
  139. }
  140.  
  141.  
  142.  
  143. PRIVATE int log_common( logmask, buf, bufsize, cp )
  144.     mask_t *logmask ;
  145.     char *buf ;
  146.     int bufsize ;
  147.     connection_s *cp ;
  148. {
  149.     register int len = 0 ;
  150.     int cc ;
  151.  
  152.    if ( M_IS_SET( *logmask, LO_HOST ) )
  153.    {
  154.       cc = strx_nprint( &buf[ len ], bufsize, " from=%s",
  155.                     conn_address( cp ) ?
  156.                     inet_ntoa( conn_address( cp )->sin_addr ) : "UNKNOWN" ) ;
  157.       len += cc ;
  158.         bufsize -= cc ;
  159.    }
  160.  
  161.    if ( M_IS_SET( *logmask, LO_TIME ) )
  162.    {
  163.       time_t current_time ;
  164.       struct tm *tmp ;
  165.  
  166.         (void) time( ¤t_time ) ;
  167.       tmp = localtime( ¤t_time ) ;
  168.       cc = strx_nprint( &buf[ len ], bufsize, " time=%d/%d/%d@%02d:%02d:%02d",
  169.                            tmp->tm_year, tmp->tm_mon+1, tmp->tm_mday,
  170.                            tmp->tm_hour, tmp->tm_min, tmp->tm_sec ) ;
  171.       len += cc ;
  172.         bufsize -= cc ;
  173.    }
  174.     return( len ) ;
  175. }
  176.  
  177.  
  178.  
  179. void log_exit( serp )
  180.     register struct server *serp ;
  181. {
  182.     char buf[ LOGBUF_SIZE ] ;
  183.     int bufsize ;
  184.     register int cc ;
  185.     register struct service *sp = SERVER_SERVICE( serp ) ;
  186.     register struct service_config *scp = CONF( sp ) ;
  187.     register int len ;
  188.     char *func = "log_exit" ;
  189.  
  190.     if ( ! SVC_IS_LOGGING( sp ) )
  191.         return ;
  192.  
  193.     bufsize = sizeof( buf ) ;
  194.     len = 0 ;
  195.  
  196.     cc = strx_nprint( buf, bufsize, "EXIT: %s", scp->id ) ;
  197.     bufsize -= cc ;
  198.     len += cc ;
  199.  
  200.     /*
  201.      * If the EXIT flag was used, log the exit status or the signal that
  202.      * killed the process. We assume that these are the only reasons
  203.      * for process termination.
  204.      */
  205.     if ( M_IS_SET( scp->log_on_success, LO_EXIT ) )
  206.     {
  207.         int num ;
  208.         char *s ;
  209.  
  210.         if ( PROC_EXITED( serp->exit_status ) )
  211.         {
  212.             s = "status" ;
  213.             num = PROC_EXITSTATUS( serp->exit_status ) ;
  214.         }
  215.         else if ( PROC_SIGNALED( serp->exit_status ) )
  216.         {
  217.             s = "signal" ;
  218.             num = PROC_TERMSIG( serp->exit_status ) ;
  219.         }
  220.         else
  221.         {
  222.             msg( LOG_ERR, func, "Bad exit status" ) ;
  223.             s = NULL ;
  224.         }
  225.  
  226.         if ( s )
  227.         {
  228.             cc = strx_nprint( &buf[ len ], bufsize, " %s=%d", s, num ) ;
  229.             len += cc ;
  230.             bufsize -= cc ;
  231.         }
  232.     }
  233.  
  234.     if ( M_IS_SET( scp->log_on_success, LO_PID ) )
  235.     {
  236.         cc = strx_nprint( &buf[ len ], bufsize, " pid=%d", serp->pid ) ;
  237.         len += cc ;
  238.         bufsize -= cc ;
  239.     }
  240.  
  241.     if ( M_IS_SET( scp->log_on_success, LO_DURATION ) )
  242.     {
  243.         time_t current_time ;
  244.  
  245.         (void) time( ¤t_time ) ;
  246.         cc = strx_nprint( &buf[ len ], bufsize, " duration=%d(sec)", 
  247.                                     current_time - serp->start_time ) ;
  248.         len += cc ;
  249.         bufsize -= cc ;
  250.     }
  251.     xlog_write( SDATA( sp )->log_handle, buf, len, XLOG_NOFLAGS ) ;
  252. }
  253.  
  254.  
  255.  
  256. /*
  257.  * Used by other parts of xinetd that want to log something without
  258.  * going through the proper channels (i.e. log_{success,failure} and log_exit)
  259.  */
  260. /* VARARGS3 */
  261. void logprint( sp, line_id, fmt, va_alist )
  262.     register struct service *sp ;
  263.     char *line_id ;
  264.     char *fmt ;
  265.     va_dcl
  266. {
  267.     struct service_config *scp = CONF( sp ) ;
  268.     char buf[ LOGBUF_SIZE ] ;
  269.     int bufsize = sizeof( buf ) ;
  270.     int len ;
  271.     int cc ;
  272.     va_list ap ;
  273.  
  274.     if ( ! SVC_IS_LOGGING( sp ) )
  275.         return ;
  276.  
  277.     len = strx_nprint( buf, bufsize, "%s: %s ", line_id, scp->id ) ;
  278.     va_start( ap ) ;
  279.     cc = strx_nprintv( &buf[ len ], bufsize, fmt, ap ) ;
  280.     va_end( ap ) ;
  281.     xlog_write( SDATA( sp )->log_handle, buf, len+cc, XLOG_NO_SIZECHECK ) ;
  282. }
  283.  
  284.